//
//  FQDMailRegisterViewController.m
//  FakeQDaily
//
//  Created by mewxiv on 9/6/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDMailRegisterViewController.h"
#import "FQDNavigationController.h"
#import "FQDInfoWebViewController.h"
#import "FQDCompleteUserInfoViewController.h"
#import "QDailyAPIConstants.h"
#import "FQDAccountInputField.h"
#import "FQDNavigationView.h"
#import "MBProgressHUD+FQDExtension.h"
#import "NSString+FQDValidation.h"
#import "QDailyAPI.h"

#import "Masonry.h"

@interface FQDMailRegisterViewController () <UITextFieldDelegate>

// QIcon图标
@property (nonatomic, strong) UIImageView *qIconImageView;
// 邮箱
@property (nonatomic, strong) FQDAccountInputField *mailInputField;
// 密码
@property (nonatomic, strong) FQDAccountInputField *passwordInputField;
// 确认密码
@property (nonatomic, strong) FQDAccountInputField *confirmPassInputField;
// 同意勾选框
@property (nonatomic, strong) UIButton *agreeCheckButton;
// 同意协议按钮
@property (nonatomic, strong) UIButton *agreeButton;
// 同意协议按钮分割线
@property (nonatomic, strong) UIView *agreeButtonSeparateLine;
// 下一步按钮
@property (nonatomic, strong) UIButton *nextStepButton;

@end

@implementation FQDMailRegisterViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.hideKeyboardOnTap = YES;
    [self setupTopUI];
    [self setupBottomUI];
}

- (void)setupTopUI
{
    self.navView.titleLabel.text = @"注册新账号";
    
    UIImage *iconImage = [UIImage imageNamed:@"c_Qdaily button"];
    self.qIconImageView = [[UIImageView alloc] init];
    self.qIconImageView.image = iconImage;
    [self.view addSubview:self.qIconImageView];
    
    WeakSelf;
    [self.qIconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).offset(90);
        make.centerX.equalTo(weakSelf.view);
        make.size.equalTo(@80);
    }];
}

- (void)setupBottomUI
{
    self.mailInputField = [[FQDAccountInputField alloc] init];
    [self.mailInputField setPlaceHolderString:@"邮箱(必填)"];
    [self.view addSubview:self.mailInputField];
    
    UITextField *mailTextField = self.mailInputField.textField;
    mailTextField.keyboardType = UIKeyboardTypeDefault;
    mailTextField.returnKeyType = UIReturnKeyDone;
    mailTextField.delegate = self;
    
    self.passwordInputField = [[FQDAccountInputField alloc] init];
    [self.passwordInputField setPlaceHolderString:@"密码(必填)"];
    [self.view addSubview:self.passwordInputField];
    
    UITextField *passwordTextField = self.passwordInputField.textField;
    passwordTextField.secureTextEntry = YES;
    passwordTextField.keyboardType = UIKeyboardTypeDefault;
    passwordTextField.returnKeyType = UIReturnKeyDone;
    passwordTextField.delegate = self;
    
    self.confirmPassInputField = [[FQDAccountInputField alloc] init];
    [self.confirmPassInputField setPlaceHolderString:@"确认密码(必填)"];
    [self.view addSubview:self.confirmPassInputField];
    
    UITextField *confirmPassTextField = self.confirmPassInputField.textField;
    confirmPassTextField.secureTextEntry = YES;
    confirmPassTextField.keyboardType = UIKeyboardTypeDefault;
    confirmPassTextField.returnKeyType = UIReturnKeyDone;
    confirmPassTextField.delegate = self;
    
    WeakSelf;
    [self.mailInputField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.qIconImageView.mas_bottom).offset(37);
        make.left.and.right.equalTo(weakSelf.view);
        make.height.equalTo(@44);
    }];
    
    [self.passwordInputField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.mailInputField.mas_bottom);
        make.left.and.right.equalTo(weakSelf.view);
        make.height.equalTo(@44);
    }];
    
    [self.confirmPassInputField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.passwordInputField.mas_bottom);
        make.left.and.right.equalTo(weakSelf.view);
        make.height.equalTo(@44);
    }];
    
    self.agreeCheckButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.agreeCheckButton setImage:[UIImage imageNamed:@"agree"] forState:UIControlStateNormal];
    [self.view addSubview:self.agreeCheckButton];
    
    [self.agreeCheckButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.confirmPassInputField.mas_bottom).offset(7);
        make.left.equalTo(weakSelf.view).offset(10);
        make.size.equalTo(@20);
    }];
    
    self.agreeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.agreeButton addTarget:self action:@selector(presentAgreement:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.agreeButton];
    
    [self.agreeButton setTitle:@"我已阅读并同意《用户注册协议》" forState:UIControlStateNormal];
    [self.agreeButton setTitleColor:[UIColor colorWithWhite:0.7f alpha:0.7f] forState:UIControlStateNormal];
    self.agreeButton.titleLabel.font = FQDRegularFontWithSize(13.0f);
    
    [self.agreeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.agreeCheckButton.mas_right);
        make.centerY.equalTo(weakSelf.agreeCheckButton);
    }];
    
    self.agreeButtonSeparateLine = [[UIView alloc] init];
    self.agreeButtonSeparateLine.backgroundColor = RGBA(178, 178, 178, 128);
    [self.view addSubview:self.agreeButtonSeparateLine];
    
    [self.agreeButtonSeparateLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.agreeButton.mas_bottom).offset(-6);
        make.right.equalTo(weakSelf.agreeButton).offset(1);
        make.size.mas_equalTo(CGSizeMake(107, 1));
    }];
    
    self.nextStepButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.nextStepButton setImage:[UIImage imageNamed:@"Next Button"] forState:UIControlStateNormal];
    [self.nextStepButton addTarget:self action:@selector(nextStep:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.nextStepButton];
    
    [self.nextStepButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.confirmPassInputField.mas_bottom).offset(44);
        make.centerX.equalTo(weakSelf.view);
        make.size.mas_equalTo(CGSizeMake(163, 44));
    }];
}

#pragma mark - Event Response

- (void)presentAgreement:(id)sender
{
    FQDInfoWebViewController *iwVC = [[FQDInfoWebViewController alloc] init];
    iwVC.title = @"用户注册协议";
    iwVC.urlString = QDailyRegisterAgreementURL;
    
    FQDNavigationController *navController = [[FQDNavigationController alloc] init];
    navController.viewControllers = @[iwVC];
    [self presentViewController:navController animated:YES completion:nil];
}

- (void)nextStep:(id)sender
{
    NSString *email = self.mailInputField.textField.text;
    if ([email isBlankString]) {
        [MBProgressHUD showMessage:@"邮箱不能为空"];
        return;
    }
    
    if (![email isValidateEmail]) {
        [MBProgressHUD showMessage:@"邮箱不合法"];
        return;
    }
    
    NSString *password = self.passwordInputField.textField.text;
    NSString *trimmedPassword = [password stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if ([trimmedPassword length] <= 0) {
        [MBProgressHUD showMessage:@"密码不能为空"];
        return;
    }
    
    NSString *confirmedPassword = self.confirmPassInputField.textField.text;
    if (![confirmedPassword isEqualToString:password]) {
        [MBProgressHUD showMessage:@"两次密码输入不一致"];
        return;
    }
    
    [MBProgressHUD showLoadingAnim];
    [[QDailyAPI sharedAPI] registerWithEmail:email password:password completion:^(id responseObject, NSError *error) {
        NSNumber *status = [responseObject valueForKeyPath:@"meta.status"];
        if ([status integerValue] == 200) {
            FQDCompleteUserInfoViewController *cuiVC = [[FQDCompleteUserInfoViewController alloc] init];
            cuiVC.blurBgImage = self.blurBgImage;
            cuiVC.email = email;
            cuiVC.password = password;
            [self.navigationController pushViewController:cuiVC animated:YES];
        } else {
            
            NSString *msg = [responseObject valueForKeyPath:@"meta.msg"];
            NSString *errorMsg = ([msg length] > 0) ? msg : @"注册失败";
            [MBProgressHUD showMessage:errorMsg];
        }
        
        [MBProgressHUD hideLoadingAnim];
    }];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

@end
